package de.thm.exa.algos;

import de.thm.exa.exceptions.HashException;
import de.thm.exa.helpers.FileWriter;
import de.thm.exa.helpers.Hash;
import de.thm.exa.helpers.Timer;
import de.thm.exa.holders.ChainedPageList;
import de.thm.exa.holders.ChainedPageMap;
import de.thm.exa.holders.Keyword;
import de.thm.exa.holders.Page;
import de.thm.exa.holders.TestResult;
import de.thm.exa.interfaces.HashAlgorithm;
import java.io.File;
import java.io.IOException;

public class ChainedHash implements HashAlgorithm
{
    private ChainedPageMap list;
    private int hashFunction = 0;
    
    private long elapsedTime = 0;
    
    private boolean succeeded = true;
    
    public ChainedHash(int flag)
    {
        list = new ChainedPageMap();
        hashFunction = flag;
    }
    
    @Override
    public void insert(Page page)
    {
        Timer.start();
        for( Keyword activeKeyword : page.getKeywords() )
        {
            insert(activeKeyword, page);
        }
        elapsedTime = Timer.stop();
    }
    
    private void insert(Keyword keyword, Page page)
    {
    	long key = Hash.generate(hashFunction, keyword);
        list.put(keyword, key, page);
    }
    
    @Override
    public ChainedPageList get(String keyword) throws HashException
    {
//        long key = Hash.generate(hashFunction, keyword);
//        return list.get(keyword, key);
        return null;
        //throw new HashException("No such key defined");
    }
    
    @Override
    public long getInserts() { return list.getInserts(); }
    @Override
    public long getCollisions() { return list.getCollisions(); }
    
    @Override
    public long getElapsedTime() { return elapsedTime; }
    @Override
    public void setElapsedTime(long time) { elapsedTime = time; }

    @Override
    public void setSucceeded(boolean succeeded) { this.succeeded = succeeded; }
    @Override
    public boolean succeeded() { return succeeded; }
    
    @Override
    public String toString()
    {
        StringBuilder description = new StringBuilder("");
        description.append("\nAlgorithm Time: ").append(elapsedTime);
        description.append("\nCollisions:     ")
                   .append(list.getCollisions());
        description.append("\nInserts:        ")
                   .append(list.getInserts());
        description.append("\nSucceeded:      ")
                   .append(succeeded);
        
        if(succeeded)
        {
            description.append("\nFunction:       ")
                       .append( Hash.toString(hashFunction) );
        }
        
        description.append("\nList 1:         ");
        if( list.isEmpty() )
        {
            description.append("Empty\n");
        } else {
            description.append( list.size() ).append(" Elements\n");
        }

        return description.toString();
    }
    
    @Override
    public void printFile(File file) throws IOException
    {
        String[] functions = new String[1];
        functions[0] = Hash.toString(hashFunction);
        
        int[] listSizes = new int[1];
        listSizes[0] = list.size();
        
        FileWriter.write(
            file,
            new TestResult( "Chained", elapsedTime, getCollisions(), getInserts(), 0, succeeded, functions, listSizes )
        );
    }
}
